package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.ExcelUtil;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.DriverRefuseRecord;
import com.ybkj.daijia.server.service.DriverRefuseRecordService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "driverrefuserecord")
public class DriverRefuseRecordController {

    private static final Logger logger = LoggerFactory
        .getLogger(DriverRefuseRecordController.class);

    @Autowired
    private DriverRefuseRecordService driverRefuseRecordService;


    @RequestMapping("list")
    public ModelAndView list() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("order") || subject.isPermitted("zhuan") || subject
            .isPermitted("freight") || subject.isPermitted("errand") || subject
            .isPermitted("zhuanxian")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("driverrefuserecord/list");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping("listData")
    public
    @ResponseBody
    Page<DriverRefuseRecord> listData(
        @RequestParam(required = false, value = "orderNumber") String orderNumber,
        @RequestParam(required = false, value = "employPhone") String employPhone,
        @RequestParam(required = false, value = "orderType") String[] orderType,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("order") || subject.isPermitted("zhuan") || subject
            .isPermitted("freight") || subject.isPermitted("errand") || subject
            .isPermitted("zhuanxian")) {

            if (null != orderType && orderType.length == 0) {
                orderType = null;
            }

            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {
                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {
                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            Page<DriverRefuseRecord> result = driverRefuseRecordService
                .listData(orderNumber, employPhone, orderType, start, end);

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "exportOrder", method = RequestMethod.GET)
    public void exportOrder(
        @RequestParam(required = false, value = "orderNumber") String orderNumber,
        @RequestParam(required = false, value = "employPhone") String employPhone,
        @RequestParam(required = false, value = "orderType") String[] orderType,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "endTime") String endTime,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException, ParseException {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("order") || subject.isPermitted("zhuan") || subject
            .isPermitted("freight") || subject.isPermitted("errand") || subject
            .isPermitted("zhuanxian")) {

            if (null != orderType && orderType.length == 0) {
                orderType = null;
            }

            Date start = null;
            Date end = null;

            if (!StringUtils.isEmpty(startTime)) {
                try {
                    start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(start);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    start = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            if (!StringUtils.isBlank(endTime)) {
                try {
                    end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(end);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);

                    end = calendar.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

            List<DriverRefuseRecord> list = driverRefuseRecordService
                .selectList(orderNumber, employPhone, orderType, start, end);

            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[6];
            head[0] = ("单号");
            head[1] = ("拒单时间");
            head[2] = ("服务人员");
            head[3] = ("服务人员电话");
            head[4] = ("类型");
            head[5] = ("备注");

            for (DriverRefuseRecord a : list) {
                List<String> celllist = new ArrayList<String>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss");
                celllist.add(a.getOrderNumber());
                celllist.add(sdf.format(a.getHappend()));
                celllist.add(a.getEmployRealName());
                celllist.add(a.getEmployPhone());
                if (a.getOrderType().toString() == "daijia") {
                    celllist.add("代驾");
                } else if (a.getOrderType().toString() == "zhuanche") {
                    celllist.add("专车");
                } else if (a.getOrderType().toString() == "freight") {
                    celllist.add("货运");
                } else if (a.getOrderType().toString() == "errand") {
                    celllist.add("跑腿");
                }
                celllist.add(a.getMemo());

                rowlist.add(celllist);
            }

            ExcelUtil.export(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

}
